﻿using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using HslCommunication.Core;
using HslCommunication.Core.Net;
using HslCommunication.Reflection;

namespace HslCommunication.Profinet.Freedom
{
	/// <summary>
	/// 基于TCP/IP协议的自由协议，需要在地址里传入报文信息，也可以传入数据偏移信息，<see cref="P:HslCommunication.Core.Net.NetworkDoubleBase.ByteTransform" />默认为<see cref="T:HslCommunication.Core.RegularByteTransform" />
	/// </summary>
	/// <example>
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\FreedomExample.cs" region="Sample1" title="实例化" />
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\FreedomExample.cs" region="Sample2" title="连接及读取" />
	/// </example>
	// Token: 0x020000A9 RID: 169
	public class FreedomTcpNet : NetworkDeviceBase
	{
		/// <summary>
		/// 实例化一个默认的对象
		/// </summary>
		// Token: 0x06000C89 RID: 3209 RVA: 0x0004A442 File Offset: 0x00048642
		public FreedomTcpNet()
		{
			base.ByteTransform = new RegularByteTransform();
		}

		/// <summary>
		/// 指定IP地址及端口号来实例化自由的TCP协议
		/// </summary>
		/// <param name="ipAddress">Ip地址</param>
		/// <param name="port">端口</param>
		// Token: 0x06000C8A RID: 3210 RVA: 0x0004A458 File Offset: 0x00048658
		public FreedomTcpNet(string ipAddress, int port)
		{
			this.IpAddress = ipAddress;
			this.Port = port;
			base.ByteTransform = new RegularByteTransform();
		}

		/// <inheritdoc />
		// Token: 0x06000C8B RID: 3211 RVA: 0x0004A480 File Offset: 0x00048680
		[HslMqttApi("ReadByteArray", "特殊的地址格式，需要采用解析包起始地址的报文，例如 modbus 协议为 stx=9;00 00 00 00 00 06 01 03 00 64 00 01")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			OperateResult<byte[], int> operateResult = FreedomTcpNet.AnalysisAddress(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadFromCoreServer(operateResult.Content1);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					bool flag3 = operateResult.Content2 >= operateResult2.Content.Length;
					if (flag3)
					{
						result = new OperateResult<byte[]>(StringResources.Language.ReceiveDataLengthTooShort);
					}
					else
					{
						result = OperateResult.CreateSuccessResult<byte[]>(operateResult2.Content.RemoveBegin(operateResult.Content2));
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000C8C RID: 3212 RVA: 0x0004A510 File Offset: 0x00048710
		public override OperateResult Write(string address, byte[] value)
		{
			return this.Read(address, 0);
		}

		/// <inheritdoc />
		// Token: 0x06000C8D RID: 3213 RVA: 0x0004A52C File Offset: 0x0004872C
		[DebuggerStepThrough]
		public override Task<OperateResult<byte[]>> ReadAsync(string address, ushort length)
		{
			FreedomTcpNet.<ReadAsync>d__4 <ReadAsync>d__ = new FreedomTcpNet.<ReadAsync>d__4();
			<ReadAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadAsync>d__.<>4__this = this;
			<ReadAsync>d__.address = address;
			<ReadAsync>d__.length = length;
			<ReadAsync>d__.<>1__state = -1;
			<ReadAsync>d__.<>t__builder.Start<FreedomTcpNet.<ReadAsync>d__4>(ref <ReadAsync>d__);
			return <ReadAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06000C8E RID: 3214 RVA: 0x0004A580 File Offset: 0x00048780
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, byte[] value)
		{
			FreedomTcpNet.<WriteAsync>d__5 <WriteAsync>d__ = new FreedomTcpNet.<WriteAsync>d__5();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<FreedomTcpNet.<WriteAsync>d__5>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06000C8F RID: 3215 RVA: 0x0004A5D2 File Offset: 0x000487D2
		public override string ToString()
		{
			return string.Format("FreedomTcpNet<{0}>[{1}:{2}]", base.ByteTransform.GetType(), this.IpAddress, this.Port);
		}

		/// <summary>
		/// 分析地址的方法，会转换成一个数据报文和数据结果偏移的信息
		/// </summary>
		/// <param name="address">地址信息</param>
		/// <returns>报文结果内容</returns>
		// Token: 0x06000C90 RID: 3216 RVA: 0x0004A5FC File Offset: 0x000487FC
		public static OperateResult<byte[], int> AnalysisAddress(string address)
		{
			OperateResult<byte[], int> result;
			try
			{
				int value = 0;
				byte[] value2 = null;
				bool flag = address.IndexOf(';') > 0;
				if (flag)
				{
					string[] array = address.Split(new char[]
					{
						';'
					}, StringSplitOptions.RemoveEmptyEntries);
					for (int i = 0; i < array.Length; i++)
					{
						bool flag2 = array[i].StartsWith("stx=");
						if (flag2)
						{
							value = Convert.ToInt32(array[i].Substring(4));
						}
						else
						{
							value2 = array[i].ToHexBytes();
						}
					}
				}
				else
				{
					value2 = address.ToHexBytes();
				}
				result = OperateResult.CreateSuccessResult<byte[], int>(value2, value);
			}
			catch (Exception ex)
			{
				result = new OperateResult<byte[], int>(ex.Message);
			}
			return result;
		}
	}
}
